package com.dk.controller;

import com.dk.mapper.TabChangeImgMapper;
import com.dk.mapper.TabRouteImgMapper;
import com.dk.pojo.*;
import com.dk.service.*;
import com.dk.utils.PageBean;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("pageController")
public class PageController {
    @Autowired
    TabChangeImgMapper tabChangeImgMapper;
    @Autowired
    TabRouteImgService tabRouteImgService;
    @Autowired
    RouteService routeService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    SellerService sellerService;
    @Autowired
    FavoriteRankService favoriteRankService;
    @Autowired
    CollectService collectService;
    @Autowired
    RedisTemplate redisTemplate;

    private TabUser isUserLogin(HttpServletRequest request){
        HttpSession session = request.getSession();
        return (TabUser) session.getAttribute("userLogin");
    }

    @RequestMapping("toIndex")
    public String toIndex(Model model, HttpServletRequest request) {
        //获取人气旅游路线 按收藏量取前四条
        List<TabRoute> routes = collectService.getInfoLimitFour();
        //获取主题旅游路线 只取前四条
        List<TabRoute> isThemeRoute = routeService.getIsThemeRouteLimitFour();
        model.addAttribute("isThemeRoute",isThemeRoute);
        //取境外旅游路线 境外游
        List<TabRoute> OverseasTravelRoutes= routeService.getInfoOverseasTravelRoute();
        model.addAttribute("OverseasTravelRoutes",OverseasTravelRoutes);
        //取最新旅游路线
        List<TabRoute> LatestTravelRoute =  routeService.getInfoLatestTravelRoute();
        model.addAttribute("LatestTravelRoute",LatestTravelRoute);
        //获取推荐旅游路线
        List<TabRoute> RecommendationRouteTravels = routeService.getInfoRecommendationRouteTravel();
        model.addAttribute("RecommendationRouteTravels",RecommendationRouteTravels);
        //获取轮播图
        List<TabChangeImg> changeImgs = tabChangeImgMapper.selectByExample(null);
        //获取国内游 按收藏取前六条
        List<TabRoute> tabRoutes = routeService.getInfoLimitSix();
        List<TabCategory> categories = categoryService.getInfo();
        TabUser userLogin = isUserLogin(request);
        if (userLogin != null) {
            model.addAttribute("user", userLogin);
        }
        model.addAttribute("popularity_routes", routes);
        model.addAttribute("tabRoutes", tabRoutes);
        model.addAttribute("categories", categories);
        model.addAttribute("changeImgs", changeImgs);
        return "index";
    }

    @RequestMapping("toMyFavorite")
    public String toMyFavorite(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", required = false, defaultValue = "8") Integer pageSize,
                               HttpServletRequest request, Model model) {

        TabUser userLogin = isUserLogin(request);;
        if (userLogin != null) {
            PageBean<TabRoute> pageInfo = collectService.getInfo(pageNum, pageSize, userLogin);
            List<TabCategory> categories = categoryService.getInfo();
            model.addAttribute("pageInfo", pageInfo);
            model.addAttribute("categories", categories);
            model.addAttribute("user",userLogin);
            return "myfavorite";
        } else {
            return "redirect:/pageController/toLogin";
        }
    }

    @RequestMapping("toLogin")
    public String toLogin(Model model) {
        List<TabCategory> categories = categoryService.getInfo();
        model.addAttribute("categories",categories);
        return "login";
    }

    @RequestMapping("toFavoriteRank")
    public String toFavoriteRank(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                 Model model,
                                 HttpServletRequest request) {
        TabUser userLogin = isUserLogin(request);
        model.addAttribute("user",userLogin);
        List<TabRoute> pageInfo = favoriteRankService.getInfoRank(pageNum, pageSize);
        List<TabCategory> categories = categoryService.getInfo();
        model.addAttribute("categories", categories);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageNum", pageNum);
        return "favoriterank";
    }

    @RequestMapping("favoriteRankPage")
    @ResponseBody
    public PageInfo<TabRoute> favoriteRankPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PageInfo<TabRoute> pageInfo = favoriteRankService.getInfo(pageNum, pageSize);
        return pageInfo;
    }

    @RequestMapping("toRegister")
    public String toRegister() {
        return "register";
    }

    @RequestMapping("toRouteList")
    public String toRouteList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                              Integer cid,
                              Model model,
                              HttpServletRequest request) {
        TabUser userLogin = isUserLogin(request);

        List<TabCategory> categories = categoryService.getInfo();
        //获取热门推荐数据
        List<TabRoute> tabRoutesPopular = routeService.getRoutePopularRecommendation(cid);//热门推荐
        //根据路线类型返回的路线集合 并做分页
        PageInfo<TabRoute> pageInfo = routeService.getInfoByCid(pageNum, pageSize, cid);
        model.addAttribute("cid", cid);
        model.addAttribute("tabRoutesPopular", tabRoutesPopular);
        model.addAttribute("categories", categories);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("user",userLogin);
        return "route_list";
    }


    @RequestMapping("toRouteDetail/{rid}/{sid}")
    public String toRouteDetail(@PathVariable("rid") Integer rid,
                                @PathVariable("sid") Integer sid,
                                HttpServletRequest request,
                                Model model) {
        HttpSession session = request.getSession();
        TabUser user = (TabUser) session.getAttribute("userLogin");
        List<TabCategory> categories = categoryService.getInfo();
        //旅游路线详情
        TabRoute tabRoute = routeService.getInfoByRid(rid);
        //访客量加一
        tabRoute.setIsaudit(tabRoute.getIsaudit() + 1);
        routeService.updateRoute(tabRoute);
        TabSeller seller = sellerService.getInfoBySid(sid);
        TabCategory category = categoryService.getInfoByCid(tabRoute.getCid());
        //获取路线对应的图片
        List<TabRouteImg> routeImgs = tabRouteImgService.getInfoByRid(rid);
        model.addAttribute("user", user);
        model.addAttribute("route", tabRoute);
        model.addAttribute("seller", seller);
        model.addAttribute("category", category);
        model.addAttribute("categories", categories);
        model.addAttribute("routeImgs", routeImgs);
        return "route_detail";
    }


}
